<template>
  <div class="login-page">
    <div class="login-header">
      <back-icon />
      <h1>登录</h1>
    </div>

    <div class="login-form">
      <div class="form-item">
        <input 
          type="text" 
          v-model="phone" 
          placeholder="请输入手机号"
          maxlength="11"
        >
      </div>
      
      <div class="form-item verification">
        <input 
          type="text" 
          v-model="code" 
          placeholder="请输入验证码"
          maxlength="6"
        >
        <button 
          class="send-code-btn" 
          :disabled="countdown > 0"
          @click="sendCode"
        >
          {{ countdown > 0 ? `${countdown}s后重新获取` : '获取验证码' }}
        </button>
      </div>

      <button class="login-btn" @click="handleLogin">登录</button>

      <div class="agreement">
        <label class="checkbox">
          <input type="checkbox" v-model="agreed">
          <span class="checkmark"></span>
        </label>
        <p class="agreement-text">
          登录即代表您已同意
          <a href="#" @click.prevent="showAgreement">《用户协议》</a>
          和
          <a href="#" @click.prevent="showPrivacy">《隐私政策》</a>
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import BackIcon from '@/components/BackIcon.vue'

const router = useRouter()
const userStore = useUserStore()
const phone = ref('')
const code = ref('')
const agreed = ref(false)
const countdown = ref(0)

// 发送验证码
const sendCode = async () => {
  if (!/^1[3-9]\d{9}$/.test(phone.value)) {
    alert('请输入正确的手机号')
    return
  }

  // 开始倒计时
  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
    }
  }, 1000)

  alert('验证码已发送')
}

// 登录
const handleLogin = async () => {
  if (!agreed.value) {
    alert('请先同意用户协议和隐私政策')
    return
  }

  if (!/^1[3-9]\d{9}$/.test(phone.value)) {
    alert('请输入正确的手机号')
    return
  }

  if (!/^\d{6}$/.test(code.value)) {
    alert('请输入6位验证码')
    return
  }

  // 模拟登录成功
  const mockToken = 'mock_token_' + Date.now()
  const mockUserInfo = {
    id: 1,
    phone: phone.value,
    nickname: '用户' + phone.value.substr(-4),
    avatar: 'https://placeholder.com/150'
  }

  // 保存token和用户信息
  userStore.setToken(mockToken)
  userStore.setUserInfo(mockUserInfo)

  alert('登录成功')
  router.replace('/my-coupons')
}

// 显示用户协议
const showAgreement = () => {
  // TODO: 显示用户协议
}

// 显示隐私政策
const showPrivacy = () => {
  // TODO: 显示隐私政策
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: #fff;
}

.login-header {
  display: flex;
  align-items: center;
  padding: 16px;
  gap: 12px;
}

.login-header h1 {
  flex: 1;
  font-size: 16px;
  font-weight: 500;
  text-align: center;
  margin-right: 24px;
}

.login-form {
  padding: 32px 16px;
}

.form-item {
  margin-bottom: 16px;
  position: relative;
}

.form-item input {
  width: 100%;
  height: 44px;
  padding: 0 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.form-item input:focus {
  border-color: #f23030;
  outline: none;
}

.verification {
  display: flex;
  gap: 12px;
}

.verification input {
  flex: 1;
}

.send-code-btn {
  width: 120px;
  height: 44px;
  background: #f23030;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
}

.send-code-btn:disabled {
  background: #ccc;
}

.login-btn {
  width: 100%;
  height: 44px;
  background: #f23030;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  margin-top: 24px;
}

.agreement {
  margin-top: 16px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.checkbox {
  position: relative;
  width: 16px;
  height: 16px;
  margin-top: 3px;
}

.checkbox input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.checkmark {
  position: absolute;
  top: 0;
  left: 0;
  width: 16px;
  height: 16px;
  border: 1px solid #ddd;
  border-radius: 2px;
}

.checkbox input:checked ~ .checkmark {
  background: #f23030;
  border-color: #f23030;
}

.checkbox input:checked ~ .checkmark:after {
  content: '';
  position: absolute;
  left: 5px;
  top: 2px;
  width: 3px;
  height: 8px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.agreement-text {
  flex: 1;
  font-size: 12px;
  color: #666;
  line-height: 1.5;
}

.agreement-text a {
  color: #f23030;
  text-decoration: none;
}
</style> 